CS4521:   Mobile and Topics in Web Programming

Android and JSON --- simple example

use org.json.* package

 

Android Example - Names

runningWant to return the following JSON string from
A web request (you app must ask for Internet permission) for URL of JSON file (but, could be returned from a Web app)


[{"firstname":"Roger","lastname":"Whitney"},
{"firstname":"Robert","lastname":"Edwards"},
{"firstname":"Kris","lastname":"Stewart"}]

public class NameList extends ListActivity {
 	  public void onCreate(Bundle savedInstanceState) {
 	  super.onCreate(savedInstanceState);
 	  setContentView(R.layout.main);
 	  setListAdapter(new ArrayAdapter<String>(this,
 	  android.R.layout.simple_list_item_1, this.fetchNetworkData()));
 	  }


public ArrayList<String> fetchNetworkData() {
    ArrayList<String> names = new ArrayList<String>();
    try {
        URL dataSource = new URL("http://Server.com/names.json"); //REQUEST JSON FILE ACROSS WEB
        URLConnection dataConnection = dataSource.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(dataConnection.getInputStream()));
        String line = in.readLine(); //READ CONTENTS OF A LINE FROM THE JSON FILE

                 JSONArray dataArray = new JSONArray(line); //Convert the String to an org.json.JSONArray object
        for (int k = 0; k < dataArray.length(); k++) {
            JSONObject name = (JSONObject) dataArray.get(k);
            names.add(name.getString("firstname") + " "+ name.getString("lastname"));
     }

    } catch (Exception e) { Log.e("rew", "error", e); }
         return names;//return the JSON Object
} } NOTE: the mention of android.R.layout.simple_list_item_1 in the onCreate method above. This is one of the many pre-built
layouts that come with Android. It is used for displaying lists ---so you don't have to re-create it in xml. built in layouts
NOTE: android.R.layout.simple_list_item_1 is <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/listItemFirstLineStyle" android:paddingTop="2dip" android:paddingBottom="3dip" android:layout_width="fill_parent" android:layout_height="wrap_content" />











android.R.layout.simple_list_item_2 is 
   
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/ 
       android" 
       android:paddingTop="2dip" 
       android:paddingBottom="2dip" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 
        <TextView android:id="@android:id/text1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       style="?android:attr/listItemFirstLineStyle"/> 

        <TextView android:id="@android:id/text2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@android:id/text1" 
       style="?android:attr/listItemSecondLineStyle" /> 
</TwoLineListItem>









AndroidManifest.xml file (asks for Internet permission)    

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="grewe.json_test" android:versionCode="1" android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".AndroidJSONActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

</manifest>

 

 

 

 

A special Note about URLConnection class ---- used above to connect to json file on web

Some people have experienced problems with Emulator and URLConnection class..........Here is a link to one such instance and solutions suggested.

i've had problems using URLConnection directly - with short transfers
i could reproducibly get "SOCKETLOG add_recv_stats 0" log error
messages, accompanied by nothing being delivered to the Android app.
i switched to using Socket or HttpClient, and everything worked fine.
URLConnection should work though IMHO.

 

 

 

 

 

 

 

© Lynne Grewe